

import { defineConfig, loadEnv } from "vite"
import { fileURLToPath, URL } from "node:url"
import { resolve, dirname } from "node:path"
import react from '@vitejs/plugin-react'
// import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite"
import type { VitePWAOptions } from "vite-plugin-pwa"
import { VitePWA } from "vite-plugin-pwa"

const PWAOptions: Partial<VitePWAOptions> = {
  injectRegister: "auto",
  registerType: "autoUpdate",
  mode: "production",
  manifest: {
    name: "PatPat",
    short_name: "PatPat",
    theme_color: "#ff2556",
    icons: [
      {
        src: "/assets/img/icons/android-chrome-192x192.png",
        sizes: "192x192",
        type: "image/png"
      },
      {
        src: "/assets/img/icons/android-chrome-512x512.png",
        sizes: "512x512",
        type: "image/png"
      }
    ],
    start_url: "/?utm_source=direct&utm_medium=pwa",
    display: "standalone",
    background_color: "#ffffff"
  },
  workbox: {
    swDest: "server/public/client/sw.js",
    skipWaiting: true,
    clientsClaim: true,
    importScripts: ["https://assets.emarsys.net/web-emarsys-sdk/3.6.0/web-emarsys-service-worker.js"],
    cacheId: "patpat-sw-cache"
  }
}

// https://vitejs.dev/config/
export default defineConfig(({ ssrBuild, mode }) => {
  const env = loadEnv(mode, process.cwd())
  return {
    base: env.VITE_PUBLIC_PATH,
    plugins: [
      react(),
      // VueI18nPlugin({
      //   include: resolve(dirname(fileURLToPath(import.meta.url)), "./src/locales/**"),
      //   runtimeOnly: false
      // })
      // VitePWA(PWAOptions)
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url))
      }
    },
    css: {
      preprocessorOptions: {
        // less: {
        //   charset: false,
        //   additionalData: '@import "./src/styles/variables.less";'
        // }
      }
    },
    define: {
      __VERSION__: JSON.stringify(process.env.npm_package_version)
    }
  }
})
